package com.dingreading.cloud.app.service.impl;

import com.dingreading.cloud.app.entity.Members;
import com.dingreading.cloud.app.entity.table.MembersTableDef;
import com.dingreading.cloud.app.mapper.MembersMapper;
import com.dingreading.cloud.app.service.MembersService;
import com.dingreading.cloud.common.util.PageUtil;
import com.dingreading.cloud.common.util.QueryFiledUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateChain;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 服务层实现。
 *
 * @author Administrator
 * @since 2023-11-22
 */
@Service
public class MembersServiceImpl extends BaseServiceImpl<MembersMapper, Members, MembersTableDef> implements MembersService {

    @Override
    protected MembersTableDef getTable() {
        return MembersTableDef.members;
    }

    @Override
    public Page<Members> pageList(PageUtil pageUtil, String keyword, String storeUid) {
        QueryCondition condition = QueryCondition.createEmpty();
        if (StringUtils.isNotBlank(storeUid))
            condition.and(table.storeUid.eq(storeUid));
        if (StringUtils.isNotBlank(keyword))
            condition.and(table.fullName.like(keyword)
                    .or(table.memberName.like(keyword))
                    .or(table.nickName.like(keyword))
                    .or(table.mob.like(keyword)));

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.id.desc());

        return getMapper().paginate(Page.of(pageUtil.getPage(), pageUtil.getRows()), wrapper);
    }

    @Override
    public Page<Members> appPage(PageUtil pageUtil, Long storeId, String fullName, String startTime, String endTime) {
        QueryCondition condition = QueryCondition.createEmpty();
        if (storeId != null)
            condition.and(table.storeId.eq(storeId));
        if (StringUtils.isNotBlank(fullName))
            condition.and(table.fullName.like(fullName));
        if (StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime))
            condition.and(table.addTime.between(QueryFiledUtil.appendMin00(startTime), QueryFiledUtil.appendMin24(endTime)));

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.id.desc());

        return getMapper().paginate(Page.of(pageUtil.getPage(), pageUtil.getRows()), wrapper);
    }

    @Override
    public Members getByUid(String storeUid, String uid) {
        QueryCondition condition = QueryCondition.createEmpty();
        if (null != storeUid)
            condition.and(table.storeUid.eq(storeUid));
        condition.and(table.uid.eq(uid));
        return getMapper().selectOneByCondition(condition);
    }

    @Override
    public void saveFaceInfo(String memberUid, String faceImg, String faceId) {
        UpdateChain.of(Members.class)
                .set(table.faceImg, faceImg)
                .set(table.faceId, faceId)
                .set(table.faceUpStatus, 1)
                .where(table.uid.eq(memberUid))
                .update();
    }

    @Override
    public void deleteFaceInfo(String memberUid) {
        UpdateChain.of(Members.class)
                .set(table.faceImg, "")
                .set(table.faceId, "")
                .set(table.faceUpStatus, -1)
                .where(table.uid.eq(memberUid))
                .update();
    }

    @Override
    public void updateStoreStatus(String memberUid, Integer storeStatus, Integer readDays, Date storeStatusTime) {
        UpdateChain<Members> updateChain = UpdateChain.of(Members.class);
        if (readDays != null)
            updateChain.set(table.readDays, readDays);
        updateChain.set(table.storeStatus, storeStatus)
                .set(table.storeStatusTime, storeStatusTime)
                .where(table.uid.eq(memberUid))
                .update();
    }

}
